package com.shushan.mixedexam.sync;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.base.Constants;
import com.shushan.mixedexam.BaseExamFragment;
import com.shushan.mixedexam.OnAnswerSelectListener;
import com.shushan.mixedexam.bean.MutiExerciseBean;
import com.shushan.mixedexam.sync.ui.AnswerRange;
import com.shushan.mixedexam.sync.ui.FillBlankView;
import com.shushan.shushanbao.R;
import com.shushan.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: FillBlankExamFragment
 * @Desciption: //填空题fragment
 * @author: zhangshihao
 * @date: 2018-08-04
 */
public class FillBlankExamFragment extends BaseExamFragment implements OnBlankFilledListener{

    private static final String TAG = FillBlankExamFragment.class.getSimpleName();
    private static final String editTag = "@$^";
    private static final String underLines = "___";
    private static final String underLine = "_";
    private static final String answerTag = ",";

    private View view;
    private FillBlankView fillBlankView;
    private TextView tvRightAnswer;

    private MutiExerciseBean exerciseBean;
    private int examIndex = 0;
    private boolean isOnlyDisplay = false;
    private OnAnswerSelectListener onAnswerSelectListener;
    private int blankCount = 0;//需要填空的数量
    private int filledBlankCount = 0;//已填好空的数量

    public static FillBlankExamFragment newInstance(boolean isOnlyDisplay,int examIndex,MutiExerciseBean exerciseBean){
        FillBlankExamFragment examFragment = new FillBlankExamFragment();
        examFragment.setOnlyDisplay(isOnlyDisplay);
        examFragment.setExamIndex(examIndex);
        examFragment.setExerciseBean(exerciseBean);

        return examFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view == null){
            view = inflater.inflate(R.layout.fragment_fill_blank,null,false);

            initView(view);

            initData();
        }
        return view;
    }

    private void initView(View view){
        fillBlankView = view.findViewById(R.id.fill_blank_view);
        fillBlankView.setOnBlankFilledListener(this);
        tvRightAnswer = view.findViewById(R.id.tv_right_selector);
        if(!Constants.isForEditors){
            tvRightAnswer.setVisibility(View.GONE);
        }
    }

    private void initData(){
        if(exerciseBean == null){
            Log.d(TAG,"exerciseBean == null ; examIndex == "+examIndex);
            return;
        }
        setExerciseBean(exerciseBean);

        tvRightAnswer.setText(exerciseBean.answer);
        List<AnswerRange> rangeList = new ArrayList<>();
        String questionContent = exerciseBean.exercises_content;
        questionContent = questionContent.replace(editTag,underLines);
        Log.d(TAG,"questionContent == "+questionContent);
        int foreIndex = 0;
        int nextIndex = 0;
        int beginIndex = 0;
        int endIndex = 0;
        int count = 0;
        while(nextIndex >= 0) {
            do {
                if(nextIndex < 0){
                    break;
                }
                foreIndex = questionContent.indexOf(underLine, nextIndex);
                if (count == 0) {
                    beginIndex = foreIndex;
                }
                count++;
                nextIndex = questionContent.indexOf(underLine, foreIndex+1);
                if ((nextIndex - foreIndex) == 1) {
                    endIndex = nextIndex;
                }
            } while (nextIndex - foreIndex == 1);
            rangeList.add(new AnswerRange(beginIndex, endIndex+1));
            count = 0;
            blankCount++;
        }
        fillBlankView.setOnlyDisplay(isOnlyDisplay);
        fillBlankView.setData(questionContent,rangeList);
        if(isOnlyDisplay){
            String answer = exerciseBean.answer;
            String[] answerArr = answer.split(answerTag);
            if(answerArr.length > 0){
                for(int i=0;i<answerArr.length;i++){
                    fillBlankView.fillAnswer(answerArr[i],i);
                }
            }
        }
    }

    @Override
    public void onBlankFilled(List<String> answerList) {
        if(++filledBlankCount == blankCount){
            StringBuffer sb = new StringBuffer();
            for(int i=0;i<answerList.size();i++){
                sb.append(answerList.get(i));
                if(i==(answerList.size()-1)){
                    break;
                }
                sb.append(answerTag);
            }
            exerciseBean.my_option = sb.toString();
            if(onAnswerSelectListener != null) {
                onAnswerSelectListener.onAnswerSelect(examIndex, sb.toString().equals(exerciseBean.answer));
            }
        }
    }

    public MutiExerciseBean getExerciseBean() {
        return exerciseBean;
    }

    public void setExerciseBean(MutiExerciseBean exerciseBean) {
        this.exerciseBean = exerciseBean;
    }

    public int getExamIndex() {
        return examIndex;
    }

    public void setExamIndex(int examIndex) {
        this.examIndex = examIndex;
    }

    public boolean isOnlyDisplay() {
        return isOnlyDisplay;
    }

    public void setOnlyDisplay(boolean onlyDisplay) {
        isOnlyDisplay = onlyDisplay;
    }

    public void setOnAnswerSelectListener(OnAnswerSelectListener onAnswerSelectListener) {
        this.onAnswerSelectListener = onAnswerSelectListener;
    }

}
